package com.automannn.atm_back.codegenerate.generate;

import com.automannn.atm_back.codegenerate.database.ReadTable;
import com.automannn.atm_back.codegenerate.pojo.ColumnEntity;
import com.automannn.atm_back.codegenerate.pojo.CreateFileProperty;
import com.automannn.atm_back.codegenerate.util.CodeResourceUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * @author chenkh
 * @time 2020/6/27 16:30
 */
public class GenerateSimpleContext implements GenerateContext{
    private static final Log log = LogFactory.getLog(GenerateSimpleContext.class);
    private static String entityPackage = "test";
    private static String entityName = "Person";
    private static String tableName = "person";
    private static String ftlDescription = "公告";
    private static String primaryKeyPolicy = "uuid";
    private static String sequenceCode = "";
    private static String[] foreignKeys;
    private List<ColumnEntity> C = new ArrayList();
    public static int n = 1;
    private static CreateFileProperty B = new CreateFileProperty();
    private List<ColumnEntity> D = new ArrayList();
    private ReadTable E = new ReadTable();

    public GenerateSimpleContext() {
    }

    public GenerateSimpleContext(String entityPackage, String entityName, String tableName, String ftlDescription, CreateFileProperty createFileProperty, int fieldRowNum, String primaryKeyPolicy, String sequenceCode) {
        GenerateSimpleContext.entityName = entityName;
        GenerateSimpleContext.entityPackage = entityPackage;
        GenerateSimpleContext.tableName = tableName;
        GenerateSimpleContext.ftlDescription = ftlDescription;
        B = createFileProperty;
        n = fieldRowNum;
        GenerateSimpleContext.primaryKeyPolicy = primaryKeyPolicy;
        GenerateSimpleContext.sequenceCode = sequenceCode;
    }

    public GenerateSimpleContext(String entityPackage, String entityName, String tableName, String ftlDescription, CreateFileProperty createFileProperty, String primaryKeyPolicy, String sequenceCode) {
        GenerateSimpleContext.entityName = entityName;
        GenerateSimpleContext.entityPackage = entityPackage;
        GenerateSimpleContext.tableName = tableName;
        GenerateSimpleContext.ftlDescription = ftlDescription;
        B = createFileProperty;
        GenerateSimpleContext.primaryKeyPolicy = primaryKeyPolicy;
        GenerateSimpleContext.sequenceCode = sequenceCode;
    }

    public GenerateSimpleContext(String entityPackage, String entityName, String tableName, String ftlDescription, CreateFileProperty createFileProperty, String primaryKeyPolicy, String sequenceCode, String[] foreignKeys) {
        GenerateSimpleContext.entityName = entityName;
        GenerateSimpleContext.entityPackage = entityPackage;
        GenerateSimpleContext.tableName = tableName;
        GenerateSimpleContext.ftlDescription = ftlDescription;
        B = createFileProperty;
        GenerateSimpleContext.primaryKeyPolicy = primaryKeyPolicy;
        GenerateSimpleContext.sequenceCode = sequenceCode;
        GenerateSimpleContext.foreignKeys = foreignKeys;
    }

    public Map<String, Object> e() {
        Map<String, Object> data = new HashMap();
        data.put("bussiPackage", CodeResourceUtil.bussiPackage);
        data.put("entityPackage", entityPackage);
        data.put("entityName", entityName);
        data.put("tableName", tableName);
        data.put("ftl_description", ftlDescription);
        data.put(org.jeecgframework.codegenerate.util.def.a.an, CodeResourceUtil.JEECG_GENERATE_TABLE_ID);
        data.put(org.jeecgframework.codegenerate.util.def.a.ap, primaryKeyPolicy);
        data.put(org.jeecgframework.codegenerate.util.def.a.aq, sequenceCode);
        data.put("ftl_create_time", org.jeecgframework.codegenerate.util.a.a(new Date()));
        data.put("foreignKeys", foreignKeys);
        data.put(org.jeecgframework.codegenerate.util.def.a.al, StringUtils.isNotEmpty(CodeResourceUtil.JEECG_UI_FIELD_REQUIRED_NUM) ? Integer.parseInt(CodeResourceUtil.JEECG_UI_FIELD_REQUIRED_NUM) : -1);
        data.put(org.jeecgframework.codegenerate.util.def.a.ao, StringUtils.isNotEmpty(CodeResourceUtil.JEECG_UI_FIELD_SEARCH_NUM) ? Integer.parseInt(CodeResourceUtil.JEECG_UI_FIELD_SEARCH_NUM) : -1);
        data.put(org.jeecgframework.codegenerate.util.def.a.am, n);

        try {
            this.D = this.E.a(tableName);
            data.put("columns", this.D);
            this.C = this.E.readOriginalTableColumn(tableName);
            data.put("originalColumns", this.C);
            Iterator i$ = this.C.iterator();

            while(i$.hasNext()) {
                ColumnEntity c = (ColumnEntity)i$.next();
                if (c.getFieldName().toLowerCase().equals(CodeResourceUtil.JEECG_GENERATE_TABLE_ID.toLowerCase())) {
                    data.put("primary_key_type", c.getFieldType());
                }
            }
        } catch (Exception var4) {
            var4.printStackTrace();
            System.exit(-1);
        }

        long serialVersionUID = org.jeecgframework.codegenerate.util.c.D() + org.jeecgframework.codegenerate.util.c.H();
        data.put("serialVersionUID", String.valueOf(serialVersionUID));
        return data;
    }

    public void generateToFile() {
        log.info("----jeecg---Code----Generation----[单表模型:" + tableName + "]------- 生成中。。。");
        CodeFactory codeFactory = new CodeFactory();
        codeFactory.a(new b());
        if (B.isJspFlag()) {
            if ("03".equals(B.getJspMode())) {
                codeFactory.b("onetomany/jspSubTemplate.ftl", "jspList");
            } else {
                if ("01".equals(B.getJspMode())) {
                    codeFactory.b("jspTableTemplate.ftl", "jsp");
                }

                if ("02".equals(B.getJspMode())) {
                    codeFactory.b("jspDivTemplate.ftl", "jsp");
                }

                codeFactory.b("jspListTemplate.ftl", "jspList");
            }
        }

        if (B.isServiceImplFlag()) {
            codeFactory.b("serviceImplTemplate.ftl", "serviceImpl");
        }

        if (B.isServiceIFlag()) {
            codeFactory.b("serviceITemplate.ftl", "service");
        }

        if (B.isActionFlag()) {
            codeFactory.b("controllerTemplate.ftl", "controller");
        }

        if (B.isEntityFlag()) {
            codeFactory.b("entityTemplate.ftl", "entity");
        }

        log.info("----jeecg----Code----Generation-----[单表模型：" + tableName + "]------ 生成完成。。。");
    }

    public static void main(String[] args) {
        System.out.println("----jeecg--------- Code------------- Generation -----[单表模型]------- 生成中。。。");
        (new b()).generateToFile();
        System.out.println("----jeecg--------- Code------------- Generation -----[单表模型]------- 生成完成。。。");
    }

    static {
        B.setActionFlag(true);
        B.setServiceIFlag(true);
        B.setJspFlag(true);
        B.setServiceImplFlag(true);
        B.setJspMode("01");
        B.setPageFlag(true);
        B.setEntityFlag(true);
    }
}
